home *** CD-ROM | disk | FTP | other *** search
- //***********************************************************************
- //
- // PropDemo.cpp
- //
- //***********************************************************************
-
- #include <afxwin.h>
- #include <afxdlgs.h>
- #include "Resource.h"
- #include "PropDemo.h"
-
- CMyApp myApp;
-
- /////////////////////////////////////////////////////////////////////////
- // CMyApp member functions
-
- BOOL CMyApp::InitInstance ()
- {
- m_pMainWnd = new CMainWindow;
- m_pMainWnd->ShowWindow (m_nCmdShow);
- m_pMainWnd->UpdateWindow ();
- return TRUE;
- }
-
- /////////////////////////////////////////////////////////////////////////
- // CMainWindow message map and member functions
-
- BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
- ON_WM_ERASEBKGND ()
- ON_WM_PAINT ()
- ON_COMMAND (IDM_OPTIONS_PROPERTIES, OnOptionsProperties)
- ON_COMMAND (IDM_OPTIONS_EXIT, OnOptionsExit)
- ON_COMMAND (IDM_OPTIONS_ABOUT, OnOptionsAbout)
- END_MESSAGE_MAP ()
-
- CMainWindow::CMainWindow ()
- {
- m_nFillType = 1;
- m_nFillColor = 2;
- m_text = "Hello, MFC";
- m_nHeight = 72;
- m_bBold = TRUE;
- m_bItalic = TRUE;
-
- Create (NULL, "PropDemo", WS_OVERLAPPEDWINDOW, rectDefault, NULL,
- MAKEINTRESOURCE (IDR_MAINFRAME));
- }
-
- BOOL CMainWindow::OnEraseBkgnd (CDC* pDC)
- {
- CRect rect;
- GetClientRect (&rect);
-
- m_nFillType == 1 ? DoGradientFill (pDC, &rect) :
- DoSolidFill (pDC, &rect);
- return TRUE;
- }
-
- void CMainWindow::OnPaint ()
- {
- CRect rect;
- GetClientRect (&rect);
-
- CPaintDC dc (this);
- DoDrawText (&dc, &rect);
- }
-
- void CMainWindow::OnOptionsProperties ()
- {
- CMyPropertySheet ps (this);
-
- ps.m_bkgndPage.m_nFillType = m_nFillType;
- ps.m_bkgndPage.m_nFillColor = m_nFillColor;
- ps.m_textPage.m_text = m_text;
- ps.m_textPage.m_nHeight = m_nHeight;
- ps.m_textPage.m_bBold = m_bBold;
- ps.m_textPage.m_bItalic = m_bItalic;
-
- if (ps.DoModal () == IDOK) {
- m_nFillType = ps.m_bkgndPage.m_nFillType;
- m_nFillColor = ps.m_bkgndPage.m_nFillColor;
- m_text = ps.m_textPage.m_text;
- m_nHeight = ps.m_textPage.m_nHeight;
- m_bBold = ps.m_textPage.m_bBold;
- m_bItalic = ps.m_textPage.m_bItalic;
-
- Invalidate ();
- }
- }
-
- void CMainWindow::OnOptionsExit ()
- {
- SendMessage (WM_CLOSE, 0, 0);
- }
-
- void CMainWindow::OnOptionsAbout ()
- {
- CAboutDialog dlg (this);
- dlg.DoModal ();
- }
-
- void CMainWindow::ApplyNow (CMyPropertySheet* pPs)
- {
- m_nFillType = pPs->m_bkgndPage.m_nFillType;
- m_nFillColor = pPs->m_bkgndPage.m_nFillColor;
- m_text = pPs->m_textPage.m_text;
- m_nHeight = pPs->m_textPage.m_nHeight;
- m_bBold = pPs->m_textPage.m_bBold;
- m_bItalic = pPs->m_textPage.m_bItalic;
-
- Invalidate ();
- }
-
- void CMainWindow::DoSolidFill (CDC* pDC, CRect* pRect)
- {
- static COLORREF crColor[5] = {
- RGB (255, 0, 0),
- RGB ( 0, 255, 0),
- RGB ( 0, 0, 255),
- RGB (255, 0, 255),
- RGB ( 0, 255, 255),
- };
-
- CBrush brush (crColor[m_nFillColor]);
- pDC->FillRect (pRect, &brush);
- }
-
- void CMainWindow::DoGradientFill (CDC* pDC, CRect* pRect)
- {
- CBrush* pBrush[64];
- for (int i=0; i<64; i++) {
- switch (m_nFillColor) {
-
- case 0:
- pBrush[i] = new CBrush (RGB (255 - (i * 4), 0, 0));
- break;
-
- case 1:
- pBrush[i] = new CBrush (RGB (0, 255 - (i * 4), 0));
- break;
-
- case 2:
- pBrush[i] = new CBrush (RGB (0, 0, 255 - (i * 4)));
- break;
-
- case 3:
- pBrush[i] = new CBrush (RGB (255 - (i * 4), 0,
- 255 - (i * 4)));
- break;
-
- case 4:
- pBrush[i] = new CBrush (RGB (0, 255 - (i * 4),
- 255 - (i * 4)));
- break;
- }
- }
-
- int nWidth = pRect->Width ();
- int nHeight = pRect->Height ();
- CRect rect;
-
- for (i=0; i<nHeight; i++) {
- rect.SetRect (0, i, nWidth, i + 1);
- pDC->FillRect (&rect, pBrush[(i * 63) / nHeight]);
- }
-
- for (i=0; i<64; i++)
- delete pBrush[i];
- }
-
- void CMainWindow::DoDrawText (CDC* pDC, CRect* pRect)
- {
- CFont font;
- int nHeight = -((pDC->GetDeviceCaps (LOGPIXELSY) * m_nHeight) / 72);
-
- font.CreateFont (nHeight, 0, 0, 0, m_bBold ? FW_BOLD : FW_NORMAL,
- m_bItalic, 0, 0, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS,
- CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH |
- FF_DONTCARE, "Times New Roman");
-
- pDC->SetBkMode (TRANSPARENT);
- pDC->SetTextColor (RGB (255, 255, 255));
-
- CFont* pOldFont = pDC->SelectObject (&font);
- pDC->DrawText (m_text, -1, pRect, DT_SINGLELINE | DT_CENTER |
- DT_VCENTER);
-
- pDC->SelectObject (pOldFont);
- }
-
- /////////////////////////////////////////////////////////////////////////
- // CMyPropertySheet message map and member functions
-
- BEGIN_MESSAGE_MAP (CMyPropertySheet, CPropertySheet)
- ON_BN_CLICKED (ID_APPLY_NOW, OnApplyNow)
- END_MESSAGE_MAP ()
-
- CMyPropertySheet::CMyPropertySheet (CWnd* pParentWnd) :
- CPropertySheet ("Properties", pParentWnd)
- {
- AddPage (&m_bkgndPage);
- AddPage (&m_textPage);
- }
-
- void CMyPropertySheet::OnApplyNow ()
- {
- GetActivePage ()->UpdateData (TRUE);
- ((CMainWindow*) AfxGetMainWnd ())->ApplyNow (this);
-
- m_bkgndPage.SetModified (FALSE);
- m_textPage.SetModified (FALSE);
- }
-
- /////////////////////////////////////////////////////////////////////////
- // CBkgndPage message map and member functions
-
- BEGIN_MESSAGE_MAP (CBkgndPage, CPropertyPage)
- ON_CONTROL_RANGE (BN_CLICKED, IDC_SOLID, IDC_CYAN, OnChange)
- END_MESSAGE_MAP ()
-
- void CBkgndPage::OnChange (UINT nID)
- {
- SetModified (TRUE);
- }
-
- void CBkgndPage::DoDataExchange (CDataExchange* pDX)
- {
- CPropertyPage::DoDataExchange (pDX);
-
- DDX_Radio (pDX, IDC_SOLID, m_nFillType);
- DDX_Radio (pDX, IDC_RED, m_nFillColor);
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CTextPage message map and member functions
-
- BEGIN_MESSAGE_MAP (CTextPage, CPropertyPage)
- ON_EN_CHANGE (IDC_TEXT, OnChange)
- ON_EN_CHANGE (IDC_HEIGHT, OnChange)
- ON_BN_CLICKED (IDC_BOLD, OnChange)
- ON_BN_CLICKED (IDC_ITALIC, OnChange)
- END_MESSAGE_MAP ()
-
- void CTextPage::OnChange ()
- {
- SetModified (TRUE);
- }
-
- void CTextPage::DoDataExchange (CDataExchange* pDX)
- {
- CPropertyPage::DoDataExchange (pDX);
-
- DDX_Text (pDX, IDC_TEXT, m_text);
- DDX_Text (pDX, IDC_HEIGHT, m_nHeight);
- DDV_MinMaxInt (pDX, m_nHeight, 8, 144);
- DDX_Check (pDX, IDC_BOLD, m_bBold);
- DDX_Check (pDX, IDC_ITALIC, m_bItalic);
- }
-
- /////////////////////////////////////////////////////////////////////////
- // CAboutDialog message map and member functions
-
- BEGIN_MESSAGE_MAP (CAboutDialog, CDialog)
- ON_WM_PAINT ()
- END_MESSAGE_MAP ()
-
- BOOL CAboutDialog::OnInitDialog ()
- {
- CDialog::OnInitDialog ();
-
- CStatic* pStatic = (CStatic*) GetDlgItem (IDC_ICONRECT);
- pStatic->GetWindowRect (&m_rect);
- pStatic->DestroyWindow ();
- ScreenToClient (&m_rect);
-
- return TRUE;
- }
-
- void CAboutDialog::OnPaint ()
- {
- CPaintDC dc (this);
- HICON hIcon = (HICON) ::GetClassLong (AfxGetMainWnd ()->m_hWnd,
- GCL_HICON);
-
- if (hIcon != NULL) {
- CDC dcMem;
- dcMem.CreateCompatibleDC (&dc);
-
- CBitmap bitmap;
- bitmap.CreateCompatibleBitmap (&dc, 32, 32);
- CBitmap* pOldBitmap = dcMem.SelectObject (&bitmap);
-
- CBrush brush (::GetSysColor (COLOR_3DFACE));
- dcMem.FillRect (CRect (0, 0, 32, 32), &brush);
- dcMem.DrawIcon (0, 0, hIcon);
-
- dc.StretchBlt (m_rect.left, m_rect.top, m_rect.Width(),
- m_rect.Height (), &dcMem, 0, 0, 32, 32, SRCCOPY);
-
- dcMem.SelectObject (pOldBitmap);
- }
- }
-